home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12300 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: lantana.singnet.com.sg!usenet
  2. From: s8700055@singnet.com.sg (XY Xie)
  3. Newsgroups: comp.lang.c
  4. Subject: Question on 'bubble sort'
  5. Date: Sat, 30 Mar 1996 05:59:44 GMT
  6. Organization: Singapore Telecom Internet Service
  7. Message-ID: <4jieso$juc@lantana.singnet.com.sg>
  8. NNTP-Posting-Host: ts900-2816.singnet.com.sg
  9. X-Newsreader: Forte Free Agent 1.0.82
  10.  
  11. I came across this sorting algorithm called 'bubble sort' in a book.
  12. Here is a part of the book's code for sorting an array of integers in
  13. ascending order :
  14.  
  15. for(outer=0 ; outer<(NUM-1) ; outer++)
  16. {
  17.   //variable definitions here......
  18.   int didswap = 0;   //didswap remains zero if no swap occurs below
  19.                              //my question is on 'didswap'
  20.  
  21.  
  22. for (inner=outer ; inner< NUM ; inner++)
  23.  {
  24.    if ( arrayname[outer] > arrayname[inner])
  25.    {
  26.      //code that swaps values follows
  27.      didswap = 1   //didswap = 1 if a swap took place
  28.    }
  29.  }
  30.  
  31. if ( ! didswap)  //*returns 1 if swap took place*(relates to question)
  32.  
  33.   { break ; }     //outer for loop terminated
  34.  
  35. }
  36.  
  37. My QUESTION : if the smallest integer happens to be the first member
  38. initially , would not the 'if ( !didswap ) '  test terminate the
  39. sorting procedure prematurely , with the rest of the list unsorted ?
  40. If not , why ?
  41. Someone pls enlighten me . Pls email me direct . THANKs for the
  42. trouble . 
  43.  
  44. XY Xie
  45. s8700055@singnet.com.sg
  46.  
  47.